﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
//using LYM.CommonWeb;

namespace CMS.BLL
{
    public class EPioneerDataBLL
    {
        DAL.EPioneerDataDAL dal = new DAL.EPioneerDataDAL();

        /// <summary>
        /// 更新EM_Web_News
        /// </summary>
        /// <param name="list">需要更新的ID列表</param>
        /// <param name="tyid">类型id</param>
        /// <param name="isPub">是否发布(0取消发布;1发布)</param>
        /// <param name="isChos">是否精选(0取消精选;1精选)</param>
        /// <returns></returns>
        public bool UpdateNews(string[] list, string tyid, string isPub, string isChos)
        {
            return dal.UpdateNews(list, tyid, isPub, isChos);
        }

        /// <summary>
        /// 更新标题
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tyid"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool UpdateNews(string id, string tyid, string title)
        {
            return dal.UpdateNews(id, tyid, title);
        }

        /// <summary>
        /// 异动板块emweb_new56
        /// </summary>
        /// <returns></returns>
        public List<Entity.EPioneerDataModel> GetDataModelPagination(string title, string id, string date, string edate)
        {
            List<Entity.EPioneerDataModel> list = new List<Entity.EPioneerDataModel>();
            string strWhere = "n_subclassid='56'";
            if (!string.IsNullOrEmpty(title))
                strWhere += " and n_title='" + title + "'";
            if (!string.IsNullOrEmpty(id))
                strWhere += " and n_id='" + id + "'";
            if (!string.IsNullOrEmpty(date))
                strWhere += " and n_modifyDate>='" + date + "'";
            if (!string.IsNullOrEmpty(edate))
                strWhere += " and n_modifyDate<='" + edate + "'";
            DataSet ds = dal.GetDataModelPagination("*", "n_modifyDate", 200, 1, 0, 1, strWhere, 200, true);
            list = GetListModel(ds, "56");
            return list;
        }

        /// <summary>
        /// 异动板块emweb_new
        /// </summary>
        /// <returns></returns>
        public List<Entity.EPioneerDataModel> GetDataModelPagination(string typeId, string title, string id, string date, string edate, int total)
        {
            List<Entity.EPioneerDataModel> list = new List<Entity.EPioneerDataModel>();
            string strWhere = "n_subclassid='" + typeId + "'";
            if (!string.IsNullOrEmpty(title))
                strWhere += " and n_title='" + title + "'";
            if (!string.IsNullOrEmpty(id))
                strWhere += " and n_id='" + id + "'";
            if (!string.IsNullOrEmpty(date))
                strWhere += " and n_modifyDate>='" + date + "'";
            if (!string.IsNullOrEmpty(edate))
                strWhere += " and n_modifyDate<='" + edate + "'";
            DataSet ds = dal.GetDataModelPagination("*", "n_modifyDate", total, 1, 0, 1, strWhere, total, true);
            list = GetListModel(ds, typeId);
            return list;
        }
        /// <summary>
        /// 个股异动
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="title"></param>
        /// <param name="id"></param>
        /// <param name="date"></param>
        /// <param name="edate"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<Entity.EPioneerDataModel> GetSTOCKABNORMALMSG(string typeId, string title, string id, string date, string edate)
        {
            List<Entity.EPioneerDataModel> modelList = new List<Entity.EPioneerDataModel>();
            if (string.IsNullOrEmpty(date))
            {
                date = DateTime.Now.ToShortDateString();
            }
            if (string.IsNullOrEmpty(edate))
            {
                edate = DateTime.Now.AddDays(1).ToShortDateString();
            }
            string strWhere = " where modify_time>='" + date + "' and modify_time<'" + edate + "' ";
            if (!string.IsNullOrEmpty(title))
                strWhere += " and case_summary='" + title + "' ";
            if (!string.IsNullOrEmpty(id))
                strWhere += " and id='" + id + "' ";

            DataTable dt = dal.GetSTOCKABNORMALMSG(strWhere);
            int rowsCount = dt.Rows.Count;
            DataTable dtEM = dal.GetEMWebNews(typeId);
            if (rowsCount > 0)
            {
                Entity.EPioneerDataModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Entity.EPioneerDataModel();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.n_id = dt.Rows[n]["id"].ToString();
                    }
                    if (dt.Rows[n]["modify_time"] != null && dt.Rows[n]["modify_time"].ToString() != "")
                    {
                        model.n_modifyDate = Convert.ToDateTime(dt.Rows[n]["modify_time"]);
                    }
                    if (dt.Rows[n]["case_content"] != null && dt.Rows[n]["case_content"].ToString() != "")
                    {
                        model.n_content = dt.Rows[n]["case_content"].ToString();
                    }
                    if (dt.Rows[n]["case_date"] != null && dt.Rows[n]["case_date"].ToString() != "")
                    {
                        model.n_createdate = Convert.ToDateTime(dt.Rows[n]["case_date"]);
                    }
                    if (dtEM.Rows.Count > 0)
                    {
                        string str = "n_id='" + model.n_id + "' and n_type='" + typeId + "'";
                        DataRow[] dr = dtEM.Select(str);
                        if (dr.Count() > 0)
                        {
                            model.n_ISpublic = dr[0]["isPub"].ToString();
                            model.n_Level = dr[0]["n_level"].ToString();
                            model.n_title = dr[0]["n_title"].ToString();
                        }
                        else
                        {
                            model.n_ISpublic = "0";
                            model.n_Level = "0";
                        }
                    }
                    else
                    {
                        model.n_ISpublic = "0";
                        model.n_Level = "0";
                    }
                    if (string.IsNullOrEmpty(model.n_title))
                    {
                        model.n_title = dt.Rows[n]["stock_name"].ToString() + "：" + dt.Rows[n]["case_summary"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 要闻
        /// </summary>
        /// <param name="tyid"></param>
        /// <param name="title"></param>
        /// <param name="id"></param>
        /// <param name="date"></param>
        /// <param name="edate"></param>
        /// <returns></returns>
        public List<Entity.EPioneerDataModel> GetNewListWhere(string tyid, string title, string id, string date, string edate)
        {
            List<Entity.EPioneerDataModel> modelList = new List<Entity.EPioneerDataModel>();

            var list = dal.GetNewListWhere(tyid, title, id, date, edate);
            int rowsCount = list.Count;
            DataTable dtEM = dal.GetEMWebNews(tyid);
            if (rowsCount > 0)
            {
                Entity.EPioneerDataModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    if (n == 200)
                        break;
                    model = new Entity.EPioneerDataModel();
                    model.n_id = list[n].Id.ToString();
                    var resu = dal.GetNewListWhere(model.n_id);
                    model.n_modifyDate = resu.PTime.Value;
                    model.n_content = resu.Content;
                    model.n_createdate = resu.PTime.Value;
                    if (dtEM.Rows.Count > 0)
                    {
                        string str = "n_id='" + model.n_id + "' and n_type='" + tyid + "'";
                        DataRow[] dr = dtEM.Select(str);
                        if (dr.Count() > 0)
                        {
                            model.n_ISpublic = dr[0]["isPub"].ToString();
                            model.n_Level = dr[0]["n_level"].ToString();
                            model.n_title = dr[0]["n_title"].ToString();
                        }
                        else
                        {
                            model.n_ISpublic = "0";
                            model.n_Level = "0";
                        }
                    }
                    else
                    {
                        model.n_ISpublic = "0";
                        model.n_Level = "0";
                    }
                    if (string.IsNullOrEmpty(model.n_title))
                    {
                        model.n_title = resu.Title;
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 单条要闻
        /// </summary>
        /// <param name="tyid"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Entity.EPioneerDataModel> GetNewListWhere(string tyid, string id)
        {
            List<Entity.EPioneerDataModel> modelList = new List<Entity.EPioneerDataModel>();

            var resu = dal.GetNewListWhere(id);
            DataTable dtEM = dal.GetEMWebNews(tyid);
            if (resu != null)
            {
                Entity.EPioneerDataModel model = new Entity.EPioneerDataModel();
                model.n_id = id;
                model.n_modifyDate = resu.PTime.Value;
                model.n_content = resu.Content;
                model.n_createdate = resu.PTime.Value;
                if (dtEM.Rows.Count > 0)
                {
                    string str = "n_id='" + model.n_id + "' and n_type='" + tyid + "'";
                    DataRow[] dr = dtEM.Select(str);
                    if (dr.Count() > 0)
                    {
                        model.n_ISpublic = dr[0]["isPub"].ToString();
                        model.n_Level = dr[0]["n_level"].ToString();
                        model.n_title = dr[0]["n_title"].ToString();
                    }
                    else
                    {
                        model.n_ISpublic = "0";
                        model.n_Level = "0";
                    }
                }
                else
                {
                    model.n_ISpublic = "0";
                    model.n_Level = "0";
                }
                if (string.IsNullOrEmpty(model.n_title))
                {
                    model.n_title = resu.Title;
                }
                modelList.Add(model);
            }
            return modelList;
        }

        /// <summary>
        /// NEWS_TEXT新闻
        /// </summary>
        /// <param name="tyid">新闻类型</param>
        /// <param name="title"></param>
        /// <param name="id"></param>
        /// <param name="date"></param>
        /// <param name="edate"></param>
        /// <returns></returns>
        public List<Entity.EPioneerDataModel> GetNewsTextList(string tyid, string title, string id, string date, string edate)
        {
            List<Entity.EPioneerDataModel> modelList = new List<Entity.EPioneerDataModel>();
            string strWhere = " where ns.SortCode='" + tyid + "' ";
            if (!string.IsNullOrEmpty(title))
                strWhere += " and nt.Title='" + title + "' ";
            if (!string.IsNullOrEmpty(id))
                strWhere += " and nt.TextID='" + id + "' ";
            if (!string.IsNullOrEmpty(date))
                strWhere += " and nt.PublDate>='" + date + "' ";
            if (!string.IsNullOrEmpty(edate))
                strWhere += " and nt.PublDate<='" + edate + "' ";
            DataTable dt = dal.GetNEWSTEXT(strWhere);
            int rowsCount = dt.Rows.Count;
            DataTable dtEM = dal.GetEMWebNews(tyid);
            if (rowsCount > 0)
            {
                Entity.EPioneerDataModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Entity.EPioneerDataModel();
                    if (dt.Rows[n]["TextID"] != null && dt.Rows[n]["TextID"].ToString() != "")
                    {
                        model.n_id = dt.Rows[n]["TextID"].ToString();
                    }
                    if (dt.Rows[n]["PublDate"] != null && dt.Rows[n]["PublDate"].ToString() != "")
                    {
                        model.n_modifyDate = Convert.ToDateTime(dt.Rows[n]["PublDate"]);
                    }
                    if (dt.Rows[n]["TextContent"] != null && dt.Rows[n]["TextContent"].ToString() != "")
                    {
                        model.n_content = dt.Rows[n]["TextContent"].ToString();
                    }
                    if (dt.Rows[n]["RecTime"] != null && dt.Rows[n]["RecTime"].ToString() != "")
                    {
                        model.n_createdate = Convert.ToDateTime(dt.Rows[n]["RecTime"]);
                    }
                    if (dtEM.Rows.Count > 0)
                    {
                        string str = "n_id='" + model.n_id + "' and n_type='" + tyid + "'";
                        DataRow[] dr = dtEM.Select(str);
                        if (dr.Count() > 0)
                        {
                            model.n_ISpublic = dr[0]["isPub"].ToString();
                            model.n_Level = dr[0]["n_level"].ToString();
                            model.n_title = dr[0]["n_title"].ToString();
                        }
                        else
                        {
                            model.n_ISpublic = "0";
                            model.n_Level = "0";
                        }
                    }
                    else
                    {
                        model.n_ISpublic = "0";
                        model.n_Level = "0";
                    }
                    if (string.IsNullOrEmpty(model.n_title))
                    {
                        model.n_title = dt.Rows[n]["Title"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }


        /// <summary>
        /// NEWS_TEXT财经新闻(内地财经)020103
        /// </summary>
        /// <returns></returns>
        public List<Entity.EPioneerDataModel> GetNewsTextList(string title, string id, string date, string edate)
        {
            List<Entity.EPioneerDataModel> modelList = new List<Entity.EPioneerDataModel>();
            string strWhere = " where ns.SortCode='020103' ";
            if (!string.IsNullOrEmpty(title))
                strWhere += " and nt.Title='" + title + "' ";
            if (!string.IsNullOrEmpty(id))
                strWhere += " and nt.TextID='" + id + "' ";
            if (!string.IsNullOrEmpty(date))
                strWhere += " and nt.PublDate>='" + date + "' ";
            if (!string.IsNullOrEmpty(edate))
                strWhere += " and nt.PublDate<='" + edate + "' ";
            DataTable dt = dal.GetNEWSTEXT(strWhere);
            int rowsCount = dt.Rows.Count;
            DataTable dtEM = dal.GetEMWebNews("020103");
            if (rowsCount > 0)
            {
                Entity.EPioneerDataModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Entity.EPioneerDataModel();
                    if (dt.Rows[n]["TextID"] != null && dt.Rows[n]["TextID"].ToString() != "")
                    {
                        model.n_id = dt.Rows[n]["TextID"].ToString();
                    }
                    if (dt.Rows[n]["PublDate"] != null && dt.Rows[n]["PublDate"].ToString() != "")
                    {
                        model.n_modifyDate = Convert.ToDateTime(dt.Rows[n]["PublDate"]);
                    }

                    if (dt.Rows[n]["TextContent"] != null && dt.Rows[n]["TextContent"].ToString() != "")
                    {
                        model.n_content = dt.Rows[n]["TextContent"].ToString();
                    }
                    if (dt.Rows[n]["RecTime"] != null && dt.Rows[n]["RecTime"].ToString() != "")
                    {
                        model.n_createdate = Convert.ToDateTime(dt.Rows[n]["RecTime"]);
                    }
                    if (dtEM.Rows.Count > 0)
                    {
                        string str = "n_id='" + model.n_id + "' and n_type='020103'";
                        DataRow[] dr = dtEM.Select(str);
                        if (dr.Count() > 0)
                        {
                            model.n_ISpublic = dr[0]["isPub"].ToString();
                            model.n_Level = dr[0]["n_level"].ToString();
                            model.n_title = dr[0]["n_title"].ToString();
                        }
                        else
                        {
                            model.n_ISpublic = "0";
                            model.n_Level = "0";
                        }
                    }
                    else
                    {
                        model.n_ISpublic = "0";
                        model.n_Level = "0";
                    }
                    if (string.IsNullOrEmpty(model.n_title))
                    {
                        model.n_title = dt.Rows[n]["Title"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 异动板块emweb_new
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<Entity.EPioneerDataModel> GetListModel(DataSet ds, string typeId)
        {
            DataTable dt = dal.GetEMWebNews(typeId);
            List<Entity.EPioneerDataModel> list = new List<Entity.EPioneerDataModel>();
            if (ds.Tables.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Entity.EPioneerDataModel model = new Entity.EPioneerDataModel();
                    model.n_content = ds.Tables[0].Rows[i]["n_content"].ToString();
                    model.n_createdate = String.IsNullOrEmpty(ds.Tables[0].Rows[i]["n_createdate"].ToString()) ? DateTime.MinValue : Convert.ToDateTime(ds.Tables[0].Rows[i]["n_createdate"].ToString());
                    model.n_id = ds.Tables[0].Rows[i]["n_id"].ToString();
                    if (dt.Rows.Count > 0)
                    {
                        string strWhere = "n_id='" + model.n_id + "' and n_type='" + typeId + "'";
                        DataRow[] dr = dt.Select(strWhere);
                        if (dr.Count() > 0)
                        {
                            model.n_ISpublic = dr[0]["isPub"].ToString();
                            model.n_Level = dr[0]["n_level"].ToString();
                            model.n_title = dr[0]["n_title"].ToString();
                        }
                        else
                        {
                            model.n_ISpublic = "0";
                            model.n_Level = "0";
                        }
                    }
                    else
                    {
                        model.n_ISpublic = "0";
                        model.n_Level = "0";
                    }
                    if (string.IsNullOrEmpty(model.n_title))
                    {
                        model.n_title = ds.Tables[0].Rows[i]["n_title"].ToString();
                    }
                    model.n_modifyDate = String.IsNullOrEmpty(ds.Tables[0].Rows[i]["n_modifyDate"].ToString()) ? DateTime.MinValue : Convert.ToDateTime(ds.Tables[0].Rows[i]["n_modifyDate"].ToString());

                    list.Add(model);
                }
            }
            return list;
        }
    }
}
